<?php

//namespace controller;

require_once("model/LastStickGame.php");
require_once("view/GameView.php");

class PlayGame {

    /**
     * @var \model\LastStickGame
     */
    private $game;

    /**
     * @var \view\GameView
     */
    private $view;

    /**
     * @var string
     */
    private $message = "";


    public function __construct() {
        $this->game = new LastStickGame();
        $this->view = new GameView($this->game);

    }

    /**
    * @return String HTML
    */
    public function runGame() {
        //Handle input
        if ($this->game->isGameOver()) {
            $this->doGameOver();
        } else {
            $this->playGame();
        }

        //Generate Output
        return $this->view->show($this->message);
    }

    /**
    * Called when game is still running
    */
    private function playGame() {
        if ($this->view->playerSelectSticks()) { //FEl 2 la in view-> så att den vet att funktioonen giller i view /GameViewnu.
            try {
                $sticksDrawnByPlayer = $this->view->getNumberOfSticks(); //FEL 4 La till view-> och flytta koden till GameView
                $this->game->playerSelectsSticks($sticksDrawnByPlayer, $this->view); //khkhkhkjhjkhkjhjkh
            } catch(\Exception $e) {
                /**
                 * FEL 1:
                 * Här var det html kod <h1>Unauthorized input</h1>" så jag flytta html till
                 * GameView public $errorMessage1 rad 28 skapar view object från GameView classen hämtar errorMessage1 variabel string
                 */
                $this->message = $this->view->errorMessage1;

            }
        }
    }

    private function doGameOver() {
        if ($this->view->playerStartsOver()) { //FEl 3 la in view-> så att den vet att funktioonen giller i view/GameView nu.
            $this->game->newGame();
        }
    }

 private function returnMassage() {

     return "hej hej hora";

    }


}